﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class PoolReposity : Reposity
    {
        public IEnumerable<Return_Water_Pond_Base_Info> ListPools(int stackId)
        {
            var pools = from r in _dataContext.Return_Water_Pond_Base_Infos
                        where r.stack_id == stackId
                        select r;
            foreach (Return_Water_Pond_Base_Info pool in pools)
            {
                yield return pool;
            }
        }
        
        public Return_Water_Pond_Base_Info GetPoolById(int stackId, int poolNo)
        {
            var pools = from r in _dataContext.Return_Water_Pond_Base_Infos
                          where stackId == r.stack_id && r.pond_no == poolNo
                          select r;

            return pools.First();
        }

        public Return_Water_Pond_Base_Info CreatePool(Return_Water_Pond_Base_Info pool)
        {
            _dataContext.Return_Water_Pond_Base_Infos.InsertOnSubmit(pool);
            SubmitChanges();
            return pool;
        }

        public void DeletePools(int stackId)
        {
            var pools = from s in _dataContext.Return_Water_Pond_Base_Infos
                      where s.stack_id == stackId
                      select s;

            foreach (Return_Water_Pond_Base_Info pool in pools)
            {
                _dataContext.Return_Water_Pond_Base_Infos.DeleteOnSubmit(pool);
            }
            SubmitChanges();
        }

        public Return_Water_Pond_Base_Info EditPool(Return_Water_Pond_Base_Info pool)
        {
            SubmitChanges();
            return pool;
        }

        public IEnumerable<Return_Water_Pond_El_Area_Base_Info> ListPoolEls(int stackId)
        {
            var poolEls = from r in _dataContext.Return_Water_Pond_El_Area_Base_Infos
                        where r.stack_id == stackId
                        select r;
            foreach (Return_Water_Pond_El_Area_Base_Info poolEl in poolEls)
            {
                yield return poolEl;
            }
        }

        public IEnumerable<Return_Water_Pond_El_Area_Base_Info> GetPoolEls(int stackId, int pondNo)
        {
            var poolEls = from r in _dataContext.Return_Water_Pond_El_Area_Base_Infos
                          where r.stack_id == stackId && r.pond_no == pondNo
                          orderby r.El ascending
                          select r;
            foreach (Return_Water_Pond_El_Area_Base_Info poolEl in poolEls)
            {
                yield return poolEl;
            }
        }

        public Return_Water_Pond_El_Area_Base_Info CreatePoolEl(Return_Water_Pond_El_Area_Base_Info poolEl)
        {
            _dataContext.Return_Water_Pond_El_Area_Base_Infos.InsertOnSubmit(poolEl);
            SubmitChanges();
            return poolEl;
        }

        public void DeletePoolEls(int stackId)
        {
            var poolEls = from s in _dataContext.Return_Water_Pond_El_Area_Base_Infos
                        where s.stack_id == stackId
                        select s;

            foreach (Return_Water_Pond_El_Area_Base_Info poolEl in poolEls)
            {
                _dataContext.Return_Water_Pond_El_Area_Base_Infos.DeleteOnSubmit(poolEl);
            }
            SubmitChanges();
        }

        public IEnumerable<Runoff_Interception_Ditch_Base_Info> ListDitches(int stackId)
        {
            var ditches = from r in _dataContext.Runoff_Interception_Ditch_Base_Infos
                        where r.stack_id == stackId
                        orderby r.ditch_no ascending
                        select r;
            foreach (Runoff_Interception_Ditch_Base_Info ditch in ditches)
            {
                yield return ditch;
            }
        }

        public Runoff_Interception_Ditch_Base_Info GetDitchById(int stackId, int ditchNo)
        {
            var ditches = from r in _dataContext.Runoff_Interception_Ditch_Base_Infos
                        where stackId == r.stack_id && r.ditch_no == ditchNo
                        select r;

            return ditches.First();
        }

        public Runoff_Interception_Ditch_Base_Info CreateDitch(Runoff_Interception_Ditch_Base_Info ditch)
        {
            _dataContext.Runoff_Interception_Ditch_Base_Infos.InsertOnSubmit(ditch);
            SubmitChanges();
            return ditch;
        }

        public void DeleteDitches(int stackId)
        {
            var ditches = from s in _dataContext.Runoff_Interception_Ditch_Base_Infos
                        where s.stack_id == stackId
                        select s;

            foreach (Runoff_Interception_Ditch_Base_Info ditch in ditches)
            {
                _dataContext.Runoff_Interception_Ditch_Base_Infos.DeleteOnSubmit(ditch);
            }
            SubmitChanges();
        }
    }
}